package com.cloud.plugin

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.ModuleDependency
import org.gradle.jvm.tasks.Jar

/**
 * 描述：
 *
 * @author FineEx   2021/12/3
 * @version 1.0
 */
class UploadMaven implements Plugin<Project> {
    def publishConfig = "publishConfig"

    @Override
    void apply(Project project) {
        def publishingConfig = project.extensions.create(publishConfig, PublishConfig)

        project.apply plugin: 'maven'

        project.afterEvaluate {
            project.uploadArchives {
                repositories {
                    mavenDeployer {
                        // Define repositories for nexus
                        repository(url: publishingConfig.repoRelease) {
                            authentication(userName: publishingConfig.repoName,
                                    password: publishingConfig.repoPassword)
                        }
                        snapshotRepository(url: publishingConfig.repoSnapshot) {
                            authentication(userName: publishingConfig.repoName,
                                    password: publishingConfig.repoPassword)
                        }
                        pom.project {
                            name publishingConfig.pomName
                            groupId = publishingConfig.groupId
                            artifactId = publishingConfig.artifactId
                            version = publishingConfig.version
                            packaging 'aar'
                            description publishingConfig.pomDescription
                            url publishingConfig.pomUrl
                            licenses {
                                license {
                                    name 'The Apache Software License, Version 2.0'
                                    url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                                }
                            }
                            developers {
                                developer {
                                    id publishingConfig.devId
                                    name publishingConfig.devName
                                    email publishingConfig.devEmail
                                }
                            }
                        }

//                      Write dependencies by hand to avoid errors
                        pom.withXml {
                            Node node = asNode()
                            def dependenciesNode = node.get('dependencies')
                            def dependenciesAdded = []
                            if (dependenciesNode == null || dependenciesNode.isEmpty()) {
                                dependenciesNode = node.appendNode('dependencies')
                                project.configurations.each { conf ->
                                    conf.allDependencies.each { dependency ->
                                        dependency.artifacts.each { art ->
                                            def dependencyName = "$dependency.group:$dependency.name:$dependency.version${(art.classifier != null) ? ":$art.classifier" : ""}"
                                            if (!dependenciesAdded.contains(dependencyName)) {
                                                dependenciesAdded.add(dependencyName)
                                                def dependencyNode = dependenciesNode.appendNode('dependency')
                                                dependencyNode.appendNode('groupId', dependency.group)
                                                dependencyNode.appendNode('artifactId', dependency.name)
                                                dependencyNode.appendNode('version', dependency.version)
                                                if (art.extension != null) {
                                                    dependencyNode.appendNode('type', art.extension)
                                                }
                                                if (art.classifier != null) {
                                                    dependencyNode.appendNode('classifier', art.classifier)
                                                }

                                                if (dependency instanceof ModuleDependency
                                                        && !dependency.getExcludeRules().isEmpty()) {
                                                    def exclusionsNode = dependencyNode.appendNode('exclusions')
                                                    dependency.getExcludeRules().each { rule ->
                                                        def exclusionNode = exclusionsNode.appendNode('exclusion')
                                                        if (rule.getGroup() != null) {
                                                            exclusionNode.appendNode('groupId', rule.getGroup())
                                                        }
                                                        if (rule.getModule() != null) {
                                                            exclusionNode.appendNode('artifactId', rule.getModule())
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
